package jay.home.bet.web.struts;

import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import jay.home.bet.domain.logic.AccountService;
import jay.home.bet.domain.logic.BasicService;
import jay.home.bet.domain.logic.BetFacade;
import jay.home.bet.domain.logic.OddsService;
import jay.home.bet.domain.logic.OrderService;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 基类Action, 与spring挂载
 * 
 * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
 * @see org.springframework.web.struts.ActionSupport
 */
public abstract class BaseAction extends Action
{

	public void setServlet(ActionServlet actionServlet)
	{
		super.setServlet(actionServlet);
		if (actionServlet != null)
		{
			ServletContext servletContext = actionServlet.getServletContext();
			WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			//this.bet = (BetFacade) wac.getBean("bet");
			this.accountService = (AccountService)wac.getBean("accountService");
			this.oddsService = (OddsService)wac.getBean("oddsService");
			this.orderService = (OrderService)wac.getBean("orderService");
			this.basicService = (BasicService)wac.getBean("basicService");
		}
	}

	private OrderService orderService;
	private AccountService accountService;
	private OddsService oddsService;
	private BasicService basicService;

	protected OrderService getOrderService()
	{
		return orderService;
	}
	protected AccountService getaccountService()
	{
		return accountService;
	}
	protected OddsService getOddsService()
	{
		return oddsService;
	}
	protected BasicService getBasicService()
	{
		return basicService;
	}
	
	/**
	 * 获取请求中的页码
	 * @param request
	 * @return
	 */
	protected final int getPageNumber(HttpServletRequest request)
	{
		String pageno = request.getParameter("page");
		if(pageno == null)return 1;
		if(Pattern.matches( "[0-9]{1,3}", pageno ))
			return Integer.parseInt(pageno);
		return 1;
	}
}
